package Linux五种IO模型.reactor2;

import java.io.IOException;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author coderFu
 * @create 2022/3/8
 */
public class AcceptorDemo2 implements Runnable {

    private final Selector selector;

    private final ServerSocketChannel serverSocketChannel;

    public AcceptorDemo2(Selector selector, ServerSocketChannel serverSocketChannel){
        this.selector = selector;
        this.serverSocketChannel = serverSocketChannel;
    }


    @Override
    public void run() {

        try{
            // 接收连接
            SocketChannel clientChannel = serverSocketChannel.accept();
            clientChannel.configureBlocking(false);
            SelectionKey register = clientChannel.register(selector, SelectionKey.OP_READ);
            register.attach(new HandlerDemo2(clientChannel,register));


        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
